﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Drawing.Printing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using DrugsShopManagement.DBCLASS;

using DrugsShopManagement;
namespace DrugsShopManagement
{
    public partial class FrmDrugsBuy : DevExpress.XtraEditors.XtraForm
    {
        int flag = 0; 
        public FrmDrugsBuy(string user_no, string user_name)
        {
            InitializeComponent();
            u_no = user_no;
            u_name = user_name;
        }
        Operator op = new Operator();
        public string u_no,u_name;
        private FrmDrugsBuyDetail frmDrugsBuyDetail;

        private void FrmDrugsBuyNew_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“drugsProvider.drugsProvider”中。您可以根据需要移动或删除它。
            this.drugsProviderTableAdapter.Fill(this.drugsProvider.drugsProvider);
            //记录当前时间，Enable Timer1控件
            this.timer1.Interval = 100;
            this.timer1.Enabled = true;
            this.dateTimePicker1.Text = DateTime.Now.ToString();
            //窗体最大化
            this.WindowState = FormWindowState.Maximized;
            //获取用户工号
            this.txtBoxKPYID.Text = u_no;
            //移除xtraTabPage2
            this.xtraTabControl1.TabPages.Remove(this.xtraTabPage2);
            try
            {
                DataTable dt = new DataTable();
                string sql = "select drugsId 药品ID,drugsName 药品名称,drugsPY 拼音码,drugsSpecification 药品规格,drugsUnit 计量单位,drugsRegNum 批准文号,drugsFactory 生产厂家,drugsInPrice 进货单价,drugsSalePrice 销售单价 from drugsInfo order by drugsId";
                dt = op.getDataTable(sql, "drugsInfo");
                dataGridView1.DataSource = dt;


                this.tBDrugsBuyerName.Text = u_name;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                tbdrugsInNum.Text = "";
                tbdrugsInNum.Focus();
                DataTable dt_1 = new DataTable();
                dt_1 = op.getDataTable("select * from drugsInfo where drugsId='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'", "drugsInfo");
                this.tbdrugsID.Text = dt_1.Rows[0]["drugsId"].ToString();
                this.tbdrugsName.Text = dt_1.Rows[0]["drugsName"].ToString();
                this.tbdrugsBuyPrice.Text = dt_1.Rows[0]["drugsInPrice"].ToString();
                this.tbdrugsPY.Text = dt_1.Rows[0]["drugsPY"].ToString();
                this.tbdrugsSpecification.Text = dt_1.Rows[0]["drugsSpecification"].ToString();
                this.tbdrugsUnit.Text = dt_1.Rows[0]["drugsUnit"].ToString();
                this.tbRegNum.Text = dt_1.Rows[0]["drugsRegNum"].ToString();
                this.tbdrugsBuyFactory.Text = dt_1.Rows[0]["drugsFactory"].ToString();

                //生成采购单号
                GetDrugsBuyCode();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }

        private void textBox17_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                string sql = "select drugsId 药品ID,drugsBuyName 药品名称,drugsBuyProvider 生产厂家,drugsBuyCount 进货单价,drugsPY 拼音码,drugsSpecification 药品规格,drugsRegNum 批准文号,drugsUnit 计量单位 from drugsBuy where drugsBuyName like '%" + this.textBoxSearch.Text.Trim() + "%'or drugsPY like '%" + this.textBoxSearch.Text.ToUpper() + "%' order by drugsId";
                dt = op.getDataTable(sql, "users");
                dataGridView1.DataSource = dt;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }


        private void btnAdd_Click_1(object sender, EventArgs e)
        {
            flag = 1;
            // tbdrugsID.Enabled  = true;
            tbdrugsName.ReadOnly = false;
            tbdrugsPY.ReadOnly = false;
            tbdrugsSpecification.ReadOnly = false;
            tbdrugsUnit.ReadOnly = false;
            tbdrugsBuyFactory.ReadOnly = false;
            tbdrugsBuyPrice.ReadOnly = false;
            tbRegNum.ReadOnly = false;
            tbdrugsInNum.ReadOnly = false;
            ClearContry();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {

            string s1 = "delete from drugsBuy where drugsID='" + tbdrugsID.Text.Trim() + "'";

            using (SqlConnection con = new SqlConnection(getSqlConnection.conStr))
            {
                con.Open();
                SqlCommand com = new SqlCommand(s1, con);
                int a = com.ExecuteNonQuery();
                if (a == 1)
                {
                    if (MessageBox.Show("你确定要删除药品采购信息吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString() == "Yes")
                    {
                        SqlDataAdapter da = new SqlDataAdapter("select * from drugsBuy", con);
                        DataTable dt = new DataTable();
                        da.Fill(dt);
                        this.dataGridView1.DataSource = dt;
                    }

                }

            }
            this.ClearContry();
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
        }
        private void ClearContry()
        {
            tbdrugsID.Text = "";
            tbdrugsName.Text = "";
            tbdrugsPY.Text = "";
            tbdrugsSpecification.Text = "";
            tbdrugsUnit.Text = "";
            tbdrugsInNum.Text = "";
            tbdrugsBuyPrice.Text = "";
            tbdrugsBuyFactory.Text = "";
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (flag == 1)  //添加采购单
            {
                if (tbdrugsName.Text == "")
                {
                    MessageBox.Show("药品名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    tbdrugsName.Focus();
                    return;
                }

                if (tbdrugsBuyFactory.Text == "")
                {
                    MessageBox.Show("供货商名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    tbdrugsBuyFactory.Focus();
                    return;
                }

                //if (tbdrugsInNum.Text == "")
                //{
                //    MessageBox.Show("数量不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                //    tbdrugsInNum.Focus();
                //    tbdrugsInNum.Text = "1";
                //    return;
                //}
                if (!(Regex.IsMatch(tbdrugsInNum.Text.Trim(), "^[0-9]*$")))
                {
                    MessageBox.Show("只能是数字！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    tbdrugsInNum.Focus();
                    tbdrugsInNum.Text = "1";
                    return;
                }

                //if (cboUnit.Text == "")
                //{
                //    MessageBox.Show("计量单位不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                //    cboUnit.SelectedIndex = 0;
                //    return;
                //}

                if (tbdrugsBuyPrice.Text == "")
                {
                    MessageBox.Show("进货价格不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    tbdrugsBuyPrice.Focus();
                    tbdrugsBuyPrice.Text = "1";
                    return;
                }

                //    if (txtEmp.Text == "")
                //    {
                //        MessageBox.Show("进货人员姓名不能为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                //        txtEmp.Focus();
                //        return;
                //    }

                if (MessageBox.Show("确定要添加药品采购信息吗！", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString() == "Yes")
                {
                    //int c = 0;
                    this.ClearContry();
                    // dateTimePicker1.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
                    // dateTimePicker1.CustomFormat = "yyyy-mm-dd hh:mm:ss";
                    //int i1 = Convert.ToInt32(tbdrugsID.Text.Trim());
                    //int i2=
                    //string str = "insert into drugsBuy values ('" +Convert .ToInt32 ( tbdrugsID.Text.Trim()) + "','" + tbdrugsBuyName.Text.Trim() + "','" +
                    //    tbdrugsPY.Text.Trim() + "','" + tbdrugsSpecification.Text.Trim() + "','" + tbdrugsUnit.Text.Trim() + "','" + textBox9.Text.Trim() + "','" +
                    //   Convert .ToInt32 ( tbdrugsInNum.Text.Trim()) + "','" + tbdrugsBuyCount.Text.Trim() + "','" + tbdrugsBuyProvider.Text.Trim() + "','" + textBox2.Text.Trim() + "')";
                    string str = "exec add_drugs '" + tbdrugsID.Text.Trim() + "','" + tbdrugsName.Text.Trim() + "','" + tbdrugsPY.Text.Trim() + "','" +
                        tbdrugsSpecification.Text.Trim() + "','" + tbdrugsUnit.Text.Trim() + "','" + tbRegNum.Text.Trim() + "','" + tbdrugsInNum.Text.Trim() + "','" +
                        tbdrugsBuyPrice.Text.Trim() + "','" + tbdrugsBuyFactory.Text.Trim() + "','" + tBDrugsBuyerName.Text.Trim() + "')";
                    using (SqlConnection con = new SqlConnection(getSqlConnection.conStr))
                    {
                        con.Open();
                        SqlCommand com = new SqlCommand(str, con);
                        int c = com.ExecuteNonQuery();
                        if (c == 1)
                        {
                            MessageBox.Show("添加成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.dateTimePicker1.Text = DateTime.Now.ToString();
        }

        private void DrugsBuySure_Click(object sender, EventArgs e)
        {
            int drugsInNum;
            if (tbdrugsID.Text=="")
            {
                MessageBox.Show("请选择需要采购的药品！");
                return;
            }
            if (tbdrugsInNum.Text.Trim() == "")
            {
                MessageBox.Show("请输入采购数量！");
                return;
            }
            try
            {
                drugsInNum=Convert.ToInt32(tbdrugsInNum.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("输入采购数量格式不正确！");
                return;
            }
            try
            {
                string sql_a = String.Format("insert into drugsBuy(drugsId,drugsName,drugsPY,drugsSpecification,drugsUnit,drugsRegNum,drugsBuyNum,drugsBuyPrice,drugsSalePrice,drugsBuyFactory,drugsBuyProvider,drugsBuyStoreRoom,drugsBuyBatch,drugsBuyerName,drugsBuyDate,drugsBuyCode)values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','','{8}','{9}','','','{10}','{11}','{12}')", tbdrugsID.Text, tbdrugsName.Text, tbdrugsPY.Text, tbdrugsSpecification.Text, tbdrugsUnit.Text, tbRegNum.Text, tbdrugsInNum.Text, tbdrugsBuyPrice.Text, tbdrugsBuyFactory.Text, comBoxDrugsProvider.Text, this.tBDrugsBuyerName.Text, this.dateTimePicker1.Value, this.txtBoxCGDH.Text);    
            op.ExecuteSql(sql_a);
            MessageBox.Show("药品【" + tbdrugsName.Text.ToString() + "】采购成功！\r\n 该药品采购单号为【" + this.txtBoxCGDH .Text.ToString()+ "】", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void textBoxSearch_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                string sql = "select drugsId 药品ID,drugsName 药品名称,drugsPY 拼音码,drugsSpecification 药品规格,drugsUnit 计量单位,drugsRegNum 批准文号,drugsFactory 生产厂家,drugsInPrice 进货单价,drugsSalePrice 销售单价 from drugsInfo where drugsName like '%" + this.textBoxSearch.Text.Trim() + "%'or drugsPY like '%" + this.textBoxSearch.Text.ToUpper() + "%' order by drugsId";
                dt = op.getDataTable(sql, "users");
                dataGridView1.DataSource = dt;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }

        private void tbdrugsInNum_TextChanged(object sender, EventArgs e)
        {
            ////Convert.ToInt32(tbDrugsBuyTotalPrice.Text) = Convert.ToInt32(tbdrugsBuyPrice.Text) * Convert.ToInt32(tbdrugsInNum.Text);
            //float a = float.Parse(tbdrugsBuyPrice.Text);
            //float b = float.Parse(tbdrugsInNum.Text);
            //tbDrugsBuyTotalPrice.Text = a * b;
        }

        private void DrugsBuyDetail_Click(object sender, EventArgs e)
        {
                frmDrugsBuyDetail = new FrmDrugsBuyDetail();
                frmDrugsBuyDetail.ShowDialog();
        }

        private void comBoxDrugsProvider_TextChanged(object sender, EventArgs e)
        {
            //生成采购单号
            GetDrugsBuyCode();

            //
            this.tbdrugsInNum.Focus();
        }

        /// <summary>
        /// 生成采购单号
        /// </summary>
        private void GetDrugsBuyCode()
        {
            ////该语句获得201268格式的日期，弃用
            //string str = Convert.ToString(DateTime.Now.Date.Year) + Convert.ToString(DateTime.Now.Date.Month) + Convert.ToString(DateTime.Now.Date.Day);

            //设置dateTimePicker2可见，以便获得如20120608格式的日期
            this.dateTimePicker2.Visible = true;
            try
            {
                //获得供应商ID
                string providername, prosql, providerid;
                providername = this.comBoxDrugsProvider.Text.Trim();
                prosql = "select providerid from drugsProvider where providercompany='" + providername + "'";
                providerid = op.getDataTable(prosql).Rows[0][0].ToString();
                //生成采购单号，生成规则为：用户工号+日期+供应商ID    如：10012012060810001
                this.txtBoxCGDH.Text = this.txtBoxKPYID.Text + this.dateTimePicker2.Text + providerid;
            }
            catch (Exception )
            {
                //MessageBox.Show(ex.Message);
            }
            //设置dateTimePicker2不可见
            this.dateTimePicker2.Visible = false;
        }
    }
}
